const Mock = require("mockjs");
// 秒杀配置

const data = Mock.mock({
  "deploy|100": [{
    "id|+1": 1,
    "name": "@ctitle(3,5)", //秒杀名称
    "address": "@image(40x40,@color)", //轮播图
    "date": "@DATETIME('yyyy-MM-dd HH:mm:ss')", //创建时间
    "status|1": [1, 0], //状态
    "period": "@time()" + '-' + "@time()", //秒杀时段
  }],
});
// 秒杀商品

const data2 = Mock.mock({
  "goods|50": [{
    "id|+1": 1,
    "deploy": "@ctitle(3,5)", //配置
    "address": "@image(40x40,@color)", //图片
    "title": "@ctitle(10,20)", //活动标题
    "intro": "@cword(10,20)", //活动简介
    "cost|100-199": 1, //原价
    "spike|50-100": 1, //秒杀价
    "limit|50-100": 1, //限量
    "residue|40-60": 1, //限量剩余
    "status|1": [1, 0], //状态
    "date": "@DATETIME('yyyy-MM-dd HH:mm:ss')", //创建时间
    "period": "@time()" + '-' + "@time()", //秒杀时段
    "classId|1": [0, 1, 2, 3] //配置类型
  }],
});

Mock.mock("/getdeploy", 'post', (req) => {
  const { pageIndex, pageSize, classId, keyWord, status } =JSON.parse(req.body);
  //深拷贝 为了不修改mock原数组
  let newList = JSON.parse(JSON.stringify(data.deploy));
  //分类查询
  if (classId) {
    newList = newList.filter((item) => item.classId * 1 === classId * 1);
  }
  //模糊查询
  if (keyWord) {
    newList = newList.filter(
      (item) => item.id * 1 === keyWord * 1 || item.name.includes(keyWord)
    );
  }
  //状态查询
  if (status) {
    newList = newList.filter((item) => item.status * 1 === status * 1);
  }
  //分页
  if (pageIndex && pageSize) {
    newList = newList.slice(
      (pageIndex - 1) * pageSize,
      pageIndex * pageSize
  );
  return {
    code: 20000,
    data: newList,
    total: data.deploy.length
  }
}
})

Mock.mock("/getgoods", 'post', (req) => {
  //分类查询 模糊查询 分页
  const { pageIndex, pageSize, classId, keyWord, status } =  JSON.parse(req.body)
  //深拷贝 为了不修改mock原数组
  let newList = JSON.parse(JSON.stringify(data2.goods));
  //分类查询
  if (classId) {
    newList = newList.filter((item) => item.classId * 1 === classId * 1);
  }
  //模糊查询
  if (keyWord) {
    newList = newList.filter(
      (item) =>
        item.id * 1 === keyWord * 1 ||
        item.title.includes(keyWord) ||
        item.intro.includes(keyWord)
    );
  }
  //状态查询
  if (status) {
    newList = newList.filter((item) => item.status * 1 === status * 1);
  }
  //分页
  if (pageIndex && pageSize) {
    newList = newList.slice(
      (pageIndex - 1) * pageSize,
      pageIndex * pageSize
    );
  }
  return {
      code: 20000,
      data: newList,
      total: data2.goods.length
    }

}) 

